package cn.bellychang.db2.service;

import cn.bellychang.db2.model.CoreItemHitNumView;
import cn.bellychang.db2.repository.CoreItemHitNumRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2019/6/21
 */
@Service
public class CoreItemHitNumService {

    private static Logger LOGGER = LoggerFactory.getLogger(CoreItemHitNumService.class);

    @Autowired
    private CoreItemHitNumRepository coreItemHitNumRepository;

    @Autowired
    @Qualifier("entityManagerQuery")
    private EntityManager entityManagerQuery;

    @Autowired
    @Qualifier("entityManagerQuery")
    private EntityManager entityManagerProd;

    private static List<CoreItemHitNumView> list;

    public static List<CoreItemHitNumView> getList() {
        return list;
    }

    public static void setList(List<CoreItemHitNumView> list) {
        CoreItemHitNumService.list = list;
    }

    public List<CoreItemHitNumView> ListNohitSuggest() {
        if (list == null || list.size()<=0) {
            LOGGER.info("从T+1库中查询事项");
            list =  coreItemHitNumRepository.findAll();

            // 这种方法是可以的 但是需要改造成本
            Query nativeQuery = entityManagerQuery.createNativeQuery("select 1");
            List resultList = nativeQuery.getResultList();
            System.out.println(resultList);

            Query nativeQuery1 = entityManagerProd.createNativeQuery("select 1");
            List resultList1 = nativeQuery1.getResultList();
            System.out.println(resultList1);

            LOGGER.info("返回结果为",list);
        }
        return list;
    }
}
